package com.hjl.cookies;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;

/**
 * @author ServivE
 * @version 0.1
 * @className GetCookiesServlet
 * @description
 * @date 2024/7/27 14:10
 * @since jdk11
 */
@WebServlet(value = "/get")
public class GetCookiesServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取传过来的cookie
        Cookie[] cookies = request.getCookies();
        //遍历
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                //如果存入的cookie有中文并且编码了，获取的时候需要解码
                System.out.println(
                URLDecoder.decode(cookie.getName(), "UTF-8") + "==" +
                URLDecoder.decode(cookie.getValue(), "UTF-8")
                );
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
